package com.pos.clothes.service.serviceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pos.clothes.mapper.ClothesMapper;
import com.pos.clothes.mapper.NormsMapper;
import com.pos.clothes.pojo.Clothes;
import com.pos.clothes.pojo.Norms;
import com.pos.clothes.service.ClothesService;
import com.pos.clothes.service.NormsService;
import com.pos.clothes.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;
@Service
public class NormsServiceImpl implements NormsService {
    @Autowired
    private NormsMapper mapper;
    @Autowired
    private ClothesService service;
    @Override
    public PageInfo getNorms(int pageNum, int pageSize) throws Exception{
        PageHelper.startPage(pageNum, pageSize);
        PageInfo pageInfo = new PageInfo(mapper.getNorms());
        return pageInfo;
    }

    @Override
    public PageInfo searchNorms(Norms norms,int pageNum, int pageSize) throws Exception{
        PageHelper.startPage(pageNum, pageSize);
        PageInfo pageInfo = new PageInfo(mapper.searchNorms(norms));
        return pageInfo;
    }

    @Override
    public Integer deleteNorms(Integer id) {
        return mapper.deleteNorms(id);
    }

    @Override
    public Integer insertNorms(Norms norms,Clothes clothes) {
        //i是clothes表插入成功的标志
        int i = 0;
        int res = service.IsNothing(clothes);
        if(res==0){
            service.insertClothes(clothes);
            i = clothes.getIdclothes();
        }
        else{
            i = res;
        }
        norms.setIdclothes(i);
        if(!isSimilar(norms)){
            //norms.setIdclothes(clothes.getIdclothes());
            return mapper.insertNorms(norms);
        }else{
            return -1;
        }
    }

    @Override
    public boolean isSimilar(Norms norms) {
        List<Norms> res = mapper.isSimilar(norms);
        if (res.isEmpty()){
            return false;
        }else{
            return true;
        }
    }

    @Override
    public Integer updateNorms(Norms norms) {
        if(isSingleCode(norms.getCode()).isEmpty() || isSingleCode(norms.getCode()).get(0).getId() == norms.getId()) {
            if(!isSimilar(norms)){
                mapper.updateNorms(norms);
                return 1;
            }else{
                return -1;
            }

        }
        else{
            return -1;
        }
    }

    @Override
    public List<Norms> isSingleCode(String code) {
        return mapper.isSingleCode(code);
    }

    @Override
    public List<Integer> count_searcNorm(List<Integer> idnorms) {
        List<Integer> priceList = new ArrayList<>();
        for (int i=0;i<idnorms.size();i++){
            int a = idnorms.get(i);
             Integer price = mapper.count_searcNorm(a);
             priceList.add(price);
        }
        return priceList;
    }

    @Override
    public Integer count_updateNorms(Norms norms) {
        return mapper.count_updateNorms(norms);
    }
    @Override
    public Integer count_getNormsRemain(Norms norms){
        return mapper.count_getNormsRemain(norms);
    }
}
